package com.neuedu.oasystem.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.neuedu.oasystem.dao.INoticeDao;
import com.neuedu.oasystem.entity.Notice;
import com.neuedu.oasystem.util.DBHelper;

public class NoticeDaoImpl extends BaseDao implements INoticeDao {

	@Override
	public boolean add(Notice param) {
		Connection conn = DBHelper.openConnection();
		PreparedStatement stmt = null;
		try {
			String sql = "insert into notice(notice_title,notice_date,emp_name,emp_id,notice_content) values(?,?,0,?,?)";
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, param.getNoticeTitle());
			stmt.setTimestamp(2, new Timestamp(param.getNoticeDate().getTime()));
			stmt.setInt(3, param.getEmpId());
			stmt.setString(4, param.getNoticeContent());
			int count = stmt.executeUpdate();
			if (count == 1) {
				return true;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, stmt, conn);
		}
		return false;
	}

	@Override
	public boolean edit(Notice param) {
		Connection conn = DBHelper.openConnection();
		PreparedStatement stmt = null;
		try {
			String sql = "update notice set notice_content=?,notice_date=? where notice_title=? ";
			stmt = conn.prepareStatement(sql);
			stmt.setString(3, param.getNoticeTitle());
			stmt.setString(1, param.getNoticeContent());
			stmt.setTimestamp(2, new Timestamp(param.getNoticeDate().getTime()));
			int count = stmt.executeUpdate();
			if (count == 1) {
				return true;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, stmt, conn);
		}
		return false;
	}

	@Override
	public boolean deleteById(int id) {

		return false;
	}

	@Override
	public Notice findById(int id) {

		return null;
	}

	@Override
	public List<Notice> findAll() {
		List<Notice> list = new ArrayList<Notice>();
		Connection conn = DBHelper.openConnection();
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			conn = getConnection();
			String sql = "select notice_title,notice_date,emp_name,emp_id,notice_content from notice order by notice_date desc";
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			while (rs.next()) {
				String noticeTitle = rs.getString("notice_title");
				Timestamp noticeDate = rs.getTimestamp("notice_date");
				String empName = rs.getString("emp_name");
				int empId = rs.getInt("emp_id");
				String noticeContent = rs.getString("notice_content");
				Notice notice = new Notice(noticeTitle, noticeDate, empName, empId, noticeContent);
				list.add(notice);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(rs, stmt, conn);
		}
		return list;
	}

	@Override
	public List<Notice> findByExample(Notice param) {

		return null;
	}

	@Override
	public List<Notice> findByPager(int page, int size) {

		return null;
	}

	@Override
	public int deleteByIds(int[] ids) {

		return 0;
	}

	@Override
	public int addAll(List<Notice> useparamrs) {

		return 0;
	}

	@Override
	public int editAll(List<Notice> param) {

		return 0;
	}

	@Override
	public boolean deleteByTitle(String title) {
		Connection conn = DBHelper.openConnection();
		PreparedStatement stmt = null;
		Connection conn1 = DBHelper.openConnection();
		PreparedStatement stmt1 = null;

		try {
			String sql = "delete from notice where notice_title=?";
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, title);
			int count = stmt.executeUpdate();
			if (count == 1) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, stmt1, conn1);
		}
		return false;
	}

	@Override
	public boolean editById(int id) {
		Connection conn = DBHelper.openConnection();
		PreparedStatement stmt = null;
		try {
			String sql = "update notice set emp_name = (select emp_name from employee where emp_id=?)";

			stmt = conn.prepareStatement(sql);

			stmt.setInt(1, id);
			int count = stmt.executeUpdate();
			if (count == 1) {
				return true;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, stmt, conn);
		}
		return false;
	}

	@Override
	public boolean findByTitle(String title) {
		Connection conn = DBHelper.openConnection();
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {

			String sql = "select notice_title from notice where notice_title=?";
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, title);
			rs = stmt.executeQuery();
			if (rs.next()) {
				String noticeTitle = rs.getString(1);
				if (noticeTitle!=null) {
					return true;
				}
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(rs, stmt, conn);
		}
		return false;
	}

	@Override
	public boolean findId(int id) {
		Connection conn = DBHelper.openConnection();
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {

			String sql = "select emp_id from employee where emp_id=?";
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, id);
			rs = stmt.executeQuery();
			if (rs.next()) {
				String noticeTitle = rs.getString(1);
				if (noticeTitle!=null) {
					return true;
				}
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(rs, stmt, conn);
		}
		return false;
	}

}
